package com.emmmya.executor;

import com.emmmya.annotation.ExpressionMethod;
import com.emmmya.exception.ExpressionExcption;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 执行过滤器使用的执行器
 * @author harin
 * @version 1.0
 * @date 2022-05-06 17:08:40
 */
public class DefaultExecutor {

    /**
     * 执行静态方法
     * @param method                待执行的方法
     * @param params                参数
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object executeMethod(Method method, Object[] params) throws InvocationTargetException, IllegalAccessException {
        List<Object> paramList = null;
        if(params != null) {
            paramList = Arrays.asList(params);
        }
        return executeMethod(null,method,paramList);
    }

    /**
     * 执行方法
     * @param obj               待执行方法的对象
     * @param method            的这个方法
     * @param paramList         传入这些参数
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object executeMethod(Object obj,Method method, List<Object> paramList) throws ExpressionExcption {
        try {
            Boolean flag = calibrationParameters(method, paramList);
            if(flag) {
                if(paramList!= null && paramList.size() > 0) {
                    return method.invoke(obj, paramList.toArray());
                }else {
                    return method.invoke(obj);
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            ExpressionMethod annotation = method.getAnnotation(ExpressionMethod.class);
            String alias = annotation.alias();
            throw new ExpressionExcption("执行方法："+alias+"，抛出异常。");
        }
    }

    /**
     * 匹配方法需要传入的参数与传过来的参数是否能匹配。
     * @param method                这个方法
     * @param paramList             与这些参数
     * @return
     */
    public static Boolean calibrationParameters(Method method,List<Object> paramList){
        if(method == null ) return false;
        Class<?>[] parameterTypes = method.getParameterTypes();
        if((parameterTypes == null || parameterTypes.length == 0) && (paramList == null || paramList.size() == 0)){
            return true;
        }
        if(parameterTypes != null && paramList != null ) {
            if (parameterTypes.length == paramList.size()){
                List<Class<?>> parameterClassList = Arrays.stream(parameterTypes).collect(Collectors.toList());
                for (int i =0; i< parameterClassList.size() ;i++) {
                    Class<?> parameterType = parameterClassList.get(i);
                    Object o = paramList.get(i);
                    if(o != null) {
                        boolean calibration = parameterType.isAssignableFrom(paramList.get(i).getClass());
                        if (!calibration) return false;
                    }
                }
                return true;
            }
        }
        return false;
    }
}
